package com.lsq.tucao.express.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsq.tucao.express.bean.po.UserLike;
import com.lsq.tucao.express.dao.HeartDetailMapper;
import com.lsq.tucao.express.dao.TucaoDetailMapper;
import com.lsq.tucao.express.dao.UserLikeMapper;
import com.lsq.tucao.express.response.JsonResult;
import com.lsq.tucao.express.service.UserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private UserLikeMapper userLikeMapper;
    @Autowired
    private HeartDetailMapper heartDetailMapper;
    @Autowired
    private TucaoDetailMapper tucaoDetailMapper;

    @Override
    public JsonResult getTucaoOrHeartLikeByUserId(Integer current,Integer size,Long userId, Integer sort) {
        if(userId != null && sort != null){
            if(sort == 0 || sort ==1){
                Page<UserLike> page = new Page<>(current,size);
                QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(UserLike::getUserId,userId)
                        .eq(UserLike::getSort,sort);

                Page<UserLike> userLikePage = userLikeMapper.selectPage(page, queryWrapper);
                if(userLikePage != null){
                    return new JsonResult(userLikePage,200,"分页查询鸡汤或吐槽数据成功");
                }else {
                    return new JsonResult(userLikePage,200,"分页查询鸡汤或吐槽数据失败");
                }
            }else {
                return new JsonResult(500,"传入的分类选择的值只能为0或1");
            }
        }else {
            return new JsonResult(500,"请传入用户id以及分类选择");
        }
    }

    @Override
    public JsonResult likeTucaoOrHeart(UserLike userLike) {
        if(userLike != null){
            int insert = userLikeMapper.insert(userLike);

            //给吐槽数据或鸡汤数据的点赞数加一
            Integer update = null;
            Integer sort = userLike.getSort();
            //点赞的是吐槽数据
            if(sort == 0){
                update = tucaoDetailMapper.likeTucao(userLike.getTucaoId());
            }
            //点赞的是鸡汤数据
            else if(sort == 1){
                update = heartDetailMapper.likeHeart(userLike.getHeartId());
            }else {
                return new JsonResult(500,"传入了非法的sort值，sort值只能为0或1");
            }

            if(update != null){
                if(insert > 0 && update > 0){
                    return new JsonResult(200,"点赞成功");
                }else {
                    return new JsonResult(500,"点赞失败");
                }
            }else {
                return new JsonResult(500,"点赞失败");
            }
        }else {
            return new JsonResult(500,"请传入点赞数据");
        }
    }

    @Override
    public JsonResult cancelLikeTucaoOrHeart(Long userId, Long tucaoOrHeartId, Integer sort) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userId);

        Integer update;
        //取消点赞的是吐槽数据
        if(sort == 0){
            update = tucaoDetailMapper.cancelLikeTucao(tucaoOrHeartId);
            map.put("tucao_id",tucaoOrHeartId);
        }
        //取消点赞的是鸡汤数据
        else if(sort == 1){
            update = heartDetailMapper.cancelLikeHeart(tucaoOrHeartId);
            map.put("heart_id",tucaoOrHeartId);
        }else {
            return new JsonResult(500,"请传入有效的sort值，sort值只能为0或1");
        }

        int delete = userLikeMapper.deleteByMap(map);

        if(update != null){
            if(update > 0 && delete > 0){
                return new JsonResult(200,"取消点赞成功");
            }else {
                return new JsonResult(500,"取消点赞失败");
            }
        }else {
            return new JsonResult(500,"取消点赞失败");
        }
    }

    @Override
    public JsonResult judgeIsLike(Long userId, Long tucaoOrHeartId, Integer sort) {
        QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserLike::getUserId,userId);
        //判断的是吐槽数据
        if(sort == 0){
            queryWrapper.lambda().eq(UserLike::getTucaoId,tucaoOrHeartId);
        }
        //判断的是鸡汤数据
        else if(sort == 1){
            queryWrapper.lambda().eq(UserLike::getHeartId,tucaoOrHeartId);
        }

        List<UserLike> userLikes = userLikeMapper.selectList(queryWrapper);
        if(userLikes.size() == 1){
            return new JsonResult(200,"已点赞");
        }else {
            return new JsonResult(500,"未点赞");
        }
    }

}
